package com.xinwei.leetcode.队列;
// https://leetcode-cn.com/problems/implement-queue-using-stacks/

import java.util.Stack;

/**
 * 实现逻辑：
 * 用两个栈，instack和outstack
 * 入队时：push到instack中
 * 出队时：1、如果outstack为空，则instack的元素逐一弹出，push到outstack中，outstack弹出栈顶元素
 *       2、如果outstack不为空，则直接弹出outstack的栈顶元素
 */
public class _232_用栈实现队列 {
    Stack<Integer> instack = new Stack<>();
    Stack<Integer> outstack = new Stack<>();

    public _232_用栈实现队列() {

    }

    public void push(int x) {
        instack.push(x);
    }

    public int pop() {
        checkOutStack();
        return outstack.pop();
    }

    public int peek() {
        checkOutStack();
        return outstack.peek();
    }

    public boolean empty() {
        return instack.isEmpty() && outstack.isEmpty();
    }

    private void checkOutStack() {
        if (outstack.isEmpty()) {
            while (!instack.isEmpty()) {
                outstack.push(instack.pop());
            }
        }
    }
}
